package com.kantboot.wechat.applet.controller;

import com.kantboot.util.RestResult;
import com.kantboot.wechat.applet.config.WechatAppletConfig;
import com.kantboot.wechat.applet.entity.AuthCode2SessionEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 为了以后开发中的一体化做准备，尽量使用户向此发送请求
 */
@RestController
@RequestMapping("/wechat/applet/get")
public class GetController {

    @RequestMapping("/get_auth_code_2_session")
    public RestResult<AuthCode2SessionEntity.Result> getAuthCode2Session(
            @RequestParam("code") String code,
            @RequestParam("appid") String appid,
            @RequestParam("secret") String secret
    ){
        AuthCode2SessionEntity.Result result=
                new AuthCode2SessionEntity.Result()
                        .setWechatAppletConfig(new WechatAppletConfig().setAppid(appid).setSecret(secret))
                        .analysis(new AuthCode2SessionEntity.Param().setJs_code(code));
        return RestResult.success(result ,"获取auth_code_2_session成功");
    }

    @RequestMapping("/get_openid")
    public RestResult<String> getOpenid(
            @RequestParam("code") String code,
            @RequestParam("appid") String appid,
            @RequestParam("secret") String secret
    ){
        AuthCode2SessionEntity.Result result=
                new AuthCode2SessionEntity.Result()
                        .setWechatAppletConfig(new WechatAppletConfig().setAppid(appid).setSecret(secret))
                .analysis(new AuthCode2SessionEntity.Param().setJs_code(code));
        return RestResult.success(result.getOpenid(),"获取openid成功");
    }

    @RequestMapping("/get_unionid")
    public RestResult<String> getUnionid(
            @RequestParam("code") String code,
            @RequestParam("appid") String appid,
            @RequestParam("secret") String secret
    ){
        AuthCode2SessionEntity.Result result=
                new AuthCode2SessionEntity.Result()
                        .setWechatAppletConfig(new WechatAppletConfig().setAppid(appid).setSecret(secret))
                        .analysis(new AuthCode2SessionEntity.Param().setJs_code(code));
        return RestResult.success(result.getUnionId(),"获取unionid成功");
    }

}
